﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Data.Business;
using NLBS.Entity;
using NLBS.Data;
using NLBS.Framework;
using NLBS.Security;
using System.Web;

namespace NLBS.Views
{
    public interface IUserAction : IAction<User>
    {
        event EventHandler ValidateUserHandler;
        event EventHandler DataByName;
        bool Valid { get; set; }
        string repassword { get; set; }
    }

    public partial class UserDetailPresenter : Presenter<IUserDetailView,IUserAction>
    {
        public override void OnViewLoaded()
        {
            if (View.userID <= 0)
            {
                View.errorMsg = "变量错误";
                return;
            }

            User currentUser = UserData.GetUserByID(View.userID);
            if (currentUser == null)
            {
                View.errorMsg = "用户不存在";
                return;
            }
            View.userList = new List<User>();
            View.userList.Add(currentUser);
        }

        public override void OnActionLoaded()
        {
            this.Action.DataSelectSingle += new EventHandler(Action_DataSelectSingle);
            this.Action.DataInsert += new EventHandler(Action_DataInsert);
            this.Action.DataUpdate += new EventHandler(Action_DataUpdate);
            this.Action.DataDelete += new EventHandler(Action_DataDelete);
            this.Action.ValidateUserHandler += new EventHandler(Action_ValidateUserHandler);
            this.Action.DataByName += new EventHandler(Action_DataByName);
            base.OnActionLoaded();
        }

        void Action_DataByName(object sender, EventArgs e)
        {
            this.Action.Entity = UserData.GetUserByName(this.Action.Entity.UserName);
        }

        void Action_ValidateUserHandler(object sender, EventArgs e)
        {
            User validateUser = UserData.GetUserByName(this.Action.Entity.UserName);
            if (this.Action.Entity == null)
            {
                throw new Exception("该用户不存在");
            }
            User user=this.Action.Entity;
            // + validateUser.Salt
            this.Action.Valid = DbProvider.Instance().ValidateUser(user.UserName, StringSecurity.StringToSHA1Hash(user.Password));
            if (this.Action.Valid)
            {
                this.Action.Entity = UserData.GetUserByName(user.UserName);
                this.Action.Entity.LastVisit=DateTime.Now;
               
                //BlogInfo.Instance().CurrentUser = this.Action.Entity;
            }
        }

        void Action_DataDelete(object sender, EventArgs e)
        {
            
            this.Action.Entity = UserData.GetUserByID(this.Action.keyID);
            if (this.Action.Entity == null)
            {
                throw new Exception("该用户不存在");
            }
            User currentUser = BlogInfo.Instance().CurrentUser;
            if (currentUser.UserID != this.Action.keyID && currentUser.GroupID != 1)
                throw new Exception("没有权限删除用户");
            if (currentUser.UserID == this.Action.keyID)
            {
                throw new Exception("不可以删除自己");
            }
            UserData.DeleteUser(this.Action.keyID);
        }

        void Action_DataUpdate(object sender, EventArgs e)
        {
           // this.Action_DataSelectSingle(null, null);
           // if (this.Action.Entity == null) throw new Exception("没有此用户");
            User regiserUser = this.Action.Entity;
            User currentUser = BlogInfo.Instance().CurrentUser;
            //检测操作权限
            if (regiserUser.UserID != currentUser.UserID && currentUser.GroupID != 1)
                throw new Exception("没有权限进行此操作");
            StringSecurity.CheckSecurityCode(HttpContext.Current.Request.Form["scode"]);
            //检测旧密码
            string oldpassword = HttpContext.Current.Request.Form["oldpassword"];
            string a = currentUser.Password;
            string b = StringSecurity.StringToSHA1Hash(oldpassword);
            if (a != b)
            {
                throw new Exception("原密码错误,请重新输入");
            }

            UserData.UpdateUser(this.Action.Entity);
            //如果是当前用户,则更新用户信息
            if (regiserUser.UserID == currentUser.UserID)
            {
                BlogInfo.Instance().CurrentUser = regiserUser;
                
            }
        }

        void Action_DataSelectSingle(object sender, EventArgs e)
        {
           
            //UserData.GetUserByID(this.Action.keyID);
            this.Action.Entity = DbProvider.Instance().GetUserByID(this.Action.keyID);
            if (this.Action.Entity == null) throw new Exception("没有此用户");
        }

        void Action_DataInsert(object sender, EventArgs e)
        {
            this.Action.Entity.Password = StringSecurity.StringToSHA1Hash(this.Action.Entity.Password);
            UserData.InsertUser(this.Action.Entity);
        }
    }
}
